jQuery(function(){
	getTotalMoney();
	getTotalNoDiscountMoney();
	initEvent();
});


/**
 * 订单类型（0输出订单、1制作订单）
 */
var accountDetail = {
	accountDetailItemsDiv : "#accountDetailItemsDiv",
	template : "#template",
	proPlanIdSelect : "#divselect_ppp_proPlanId",
	proPlanIdValue : "#proPlanId",
	discountSelect : "#divselect_ppp_discount",
	discountValue : "#discount",
	count : "#count",
	unit : "#unit",
	price : "#price",
	sumMoney : "#sumMoney",
	sumNoDiscountMoney: '#sumNoDiscountMoney',
	tag:{sumMoney:'sumMoney'}
	
};

var initEvent = function(){
	var rowNumber = $(accountDetail.accountDetailItemsDiv+" li[tag='"+accountDetail.tag.sumMoney+"']").length;
	for(var i=1;i<=rowNumber;i++){
		initEventCountInputBlur(rowNumber);
		initEventPriceInputBlur(rowNumber);
		$.divselect(accountDetail.discountSelect+i, accountDetail.discountValue+i,function(txt,value){
			countMoneyOfAllItems();
		});
	}
};

var getTotalMoney = function(){
	var money=0;
	$(".c_tab_block .c_tab2 input[name='sumMoney']").each(function(index,obj){
		var sumMoney = parseFloat($(obj).val()).toFixed(2);
		money=(money+parseFloat(sumMoney));
	});
	$("#money").val(money.toFixed(2));
};

var getTotalNoDiscountMoney = function(){
	var moneyNoDiscount=0;
	$(".c_tab_block .c_tab2 input[name='sumNoDiscountMoney']").each(function(index,obj){
		var sumNoDiscountMoney = parseFloat($(obj).val()).toFixed(2);
		moneyNoDiscount=(moneyNoDiscount+parseFloat(sumNoDiscountMoney));
	});
	$("#moneyNoDiscount").val(moneyNoDiscount.toFixed(2));
};
	
var addAccountDetails = function(){
	var rowNumber = $(accountDetail.accountDetailItemsDiv+" li[tag='"+accountDetail.tag.sumMoney+"']").length+1;
	var row = $(accountDetail.template).clone();
	var proPlanIdSelect = row.find(accountDetail.proPlanIdSelect);
	var proPlanIdValue = row.find(accountDetail.proPlanIdValue);
	var discountSelect = row.find(accountDetail.discountSelect);
	var discountValue = row.find(accountDetail.discountValue);
	var count = row.find(accountDetail.count);
	var unit = row.find(accountDetail.unit);
	var price = row.find(accountDetail.price);
	var sumMoney = row.find(accountDetail.sumMoney);
	var sumNoDiscountMoney = row.find(accountDetail.sumNoDiscountMoney);
	
	proPlanIdSelect.attr("id",proPlanIdSelect.attr("id")+rowNumber);
	proPlanIdValue.attr("id",proPlanIdValue.attr("id")+rowNumber);
	discountSelect.attr("id",discountSelect.attr("id")+rowNumber);
	discountValue.attr("id",discountValue.attr("id")+rowNumber);
	
	count.attr("id",count.attr("id")+rowNumber);
	unit.attr("id",unit.attr("id")+rowNumber);
	price.attr("id",price.attr("id")+rowNumber);
	sumMoney.attr("id",sumMoney.attr("id")+rowNumber);
	sumNoDiscountMoney.attr("id",sumNoDiscountMoney.attr("id")+rowNumber);
	
	$(accountDetail.accountDetailItemsDiv).append(row.html());
	initEventRowDivSelect(rowNumber);
	initEventPriceInputBlur(rowNumber);
	initEventCountInputBlur(rowNumber);
};

var deleteMe = function(obj){
	$(obj).parent().parent().remove();
};

var initEventRowDivSelect = function(rowNumber){
	$.divselect(accountDetail.proPlanIdSelect+rowNumber, accountDetail.proPlanIdValue+rowNumber,function(txt,value){
		setAccountDetailItems(rowNumber,value);
	});
	$.divselect(accountDetail.discountSelect+rowNumber, accountDetail.discountValue+rowNumber,function(txt,value){
		countMoneyOfItems(rowNumber);
	});
};

var initEventCountInputBlur = function(rowNumber){
	$(accountDetail.count+rowNumber).on('blur',function(){
		//alert($(this).val());
		countMoneyOfItems(rowNumber);
	});
};

var initEventPriceInputBlur = function(rowNumber){
	$(accountDetail.price+rowNumber).on('blur',function(){
		countMoneyOfItems(rowNumber);
	});
};

var countMoneyOfItems = function(rowNumber){
	var count = $(accountDetail.count+rowNumber).val();
	var index = $(accountDetail.count+rowNumber).attr('index');
	var price = $(accountDetail.price+rowNumber).val();
	var discount = $(accountDetail.discountValue+rowNumber).val();
	var sumMoney = count*price*discount/(index==0?1:index);
	var sumNoDiscountMoney = count*price/(index==0?1:index);
	$(accountDetail.sumNoDiscountMoney+rowNumber).val(sumNoDiscountMoney.toFixed(2));
	$(accountDetail.sumMoney+rowNumber).val(sumMoney.toFixed(2));
	getTotalMoney();
	getTotalNoDiscountMoney();
};

var countMoneyOfAllItems = function(){
	var rowNumber = $(accountDetail.accountDetailItemsDiv+" li[tag='"+accountDetail.tag.sumMoney+"']").length;
	for(var i=1;i<=rowNumber;i++){
		countMoneyOfItems(i);
	}
};

var setAccountDetailItems = function(rowNumber,value){
	var form = {};
	form.proPlanId=value;
	ajaxCommFunWithDataType(globePath+"/producePlan/findProducePlanById.htm", form ,'json',function(data){
		if (data.result == "success") {
			var temp = data.proPlanBean.count>1?data.proPlanBean.count:'';
			$(accountDetail.unit+rowNumber).val(temp+data.proPlanBean.unit);
			$(accountDetail.count+rowNumber).attr('index',data.proPlanBean.count);
			$(accountDetail.price+rowNumber).val(data.proPlanBean.price);
			countMoneyOfItems(rowNumber);
		} else {
			layer.alert(data.result);
		};
	});
};


var saveAccount = function(){
	//validate();
	var fun = function(msg) {
		if (msg.result == "success") {
			layer.msg("核算成功！",1,9,function(){
				window.location.href=globePath+"/account/listAjax.htm";
			});
		} else {
			layer.alert(msg.result);
		};
	};
	
	ajaxCommFunWithDataType(globePath+"/account/submitPAccount.htm", $("#addAccountForm").serialize(),'json',fun);
};

var validate = function(){
	var name = $.trim($("#name").val());
	if(name==''){
		layer.msg("生产任务名称不能为空");
		return false;
	}
};